﻿//  --------------------------------
//  Copyright (c) AW Systems, Inc. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/SLAM/license
//  ---------------------------------

using System;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;

namespace AWS.SLAM.Configuration
{
	/// <summary>
	/// A SLAM XML configuration section that provides support for Type ID Definitions.
	/// </summary>
	public class XmlTypeIdDefinitionSection : TypeIdDefinitionSection
	{
		private XDocument _configXml;

		/// <summary>
		/// Gets the default ID definition stored in the ConnectionStrings configuration section.
		/// </summary>
		/// <returns></returns>
		public override TypeIdDefinition Default
		{
			get
			{
				string typeIdDefinitionName = "";
				if (_configXml.Root.Element("TypeIdDefinitions") != null)
				{
					if (SlamItemContext.ListItem != null)
					{
						IConfigurationManager configManager = ComponentService.GetInstance<IConfigurationManager>();

						if (configManager.DataMapping is XmlSqlDataMapping)
						{
							typeIdDefinitionName = ((XmlSqlDataMapping)configManager.DataMapping).GetTypeIdDefinitionName();
						}
					}

					if (String.IsNullOrEmpty(typeIdDefinitionName) && _configXml.Root.Element("TypeIdDefinitions").Attribute("Default") != null)
					{
						typeIdDefinitionName = _configXml.Root.Element("TypeIdDefinitions").Attribute("Default").Value;
					}
				}
				return GetTypeIdDefinition(typeIdDefinitionName);
			}
		}

		public XmlTypeIdDefinitionSection(XDocument configurationXml)
		{
			_configXml = configurationXml;
		}

		/// <summary>
		/// Gets an ID definition stored in the TypeIdDefinitions configuration section.
		/// </summary>
		/// <returns></returns>
		public override TypeIdDefinition GetTypeIdDefinition(string name)
		{
			TypeIdDefinition typeIdDefinition = new TypeIdDefinition();
			if (_configXml.Root.Element("TypeIdDefinitions") != null)
			{
				typeIdDefinition =	(from node in _configXml.Root.Element("TypeIdDefinitions").Elements("add")
									 where node.Attribute("Name") != null && node.Attribute("Name").Value == name
									 select new TypeIdDefinition
									 {
										 IsPrimaryKey = node.Attribute("IsPrimaryKey") == null ? false : (string)node.Attribute("IsPrimaryKey").Value.ToLower() == "true",
										 Name = node.Attribute("Name") == null ? "" : (string)node.Attribute("Name"),
										 NameFormat = node.Attribute("NameFormat") == null ? "" : (string)node.Attribute("NameFormat"),
										 ReplaceSharePointIds = node.Attribute("ReplaceSharePointIds") == null ? false : (string)node.Attribute("ReplaceSharePointIds").Value.ToLower() == "true",
										 SqlType = node.Attribute("SqlType") == null ? "" : (string)node.Attribute("SqlType"),
										 ValuePrefix = node.Attribute("ValuePrefix") == null ? "" : (string)node.Attribute("ValuePrefix"),
										 ValueSuffix = node.Attribute("ValueSuffix") == null ? "" : (string)node.Attribute("ValueSuffix"),
										 ValueFormat = node.Attribute("ValueFormat") == null ? TypeIdDefinitionValueFormat.Guid : (TypeIdDefinitionValueFormat)Enum.Parse(typeof(TypeIdDefinitionValueFormat), node.Attribute("ValueFormat").Value),
									 }).FirstOrDefault();

				if (typeIdDefinition == null)
				{
					typeIdDefinition = new TypeIdDefinition();
				}
			}

			return typeIdDefinition;
		}
	}
}
